<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: checkout.php 493 2014-01-30 16:35:53Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 493 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:35:53 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
define('GLOBAL_COLUMN_LEFT', '0');

include('includes/application_top.php');
$smarty = new Smarty();

require (DIR_WS_CLASSES.'class.checkout.php');
$checkout = new checkout();
require (DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');

require_once (DIR_FS_INC.'inc.address_label.php');
require_once (DIR_FS_INC.'inc.get_address_format_id.php');
require_once (DIR_FS_INC.'inc.check_stock.php');
require_once (DIR_FS_INC.'inc.count_shipping_modules.php');
require_once (DIR_FS_INC.'inc.parse_shipping.php');
require_once (DIR_FS_INC.'inc.array_get_item.php');

require(DIR_FS_INC.'inc.free_shipping.php');

require_once(DIR_WS_CLASSES.'class.order_total.php');
require_once(DIR_WS_CLASSES.'class.order.php');

checkout_site('checkout');
$is_xajax = (strlen($_POST['xajax']) > 4 || strlen($_POST['xjxfun']) > 4 || strlen($_GET['xajax']) > 4 || strlen($_GET['xjxfun']) > 4);

if (!$is_xajax) 
	$_SESSION['cot_gv'] = false;
	
if (!$is_xajax && $_SESSION['payment'] == 'no_payment') 
	unset($_SESSION['payment']);
	
if ($_SESSION['allow_checkout'] == 'false')
	redirect(href_link(FILENAME_SHOPPING_CART));

if (!isset ($_SESSION['customer_id']) && !$is_xajax) {
	if (ACCOUNT_OPTIONS == 'guest')
		redirect(href_link(FILENAME_CREATE_GUEST_ACCOUNT, '', 'SSL'));
	else
		redirect(href_link(FILENAME_LOGIN, '', 'SSL'));
}

if ($_SESSION['cart']->count_contents() < 1 && !$is_xajax)
	redirect(href_link(FILENAME_SHOPPING_CART));

if (!isset ($_SESSION['sendto']))
	$_SESSION['sendto'] = $_SESSION['customer_default_address_id'];
	
else {
	$check_address = $db->db_query("SELECT COUNT(*) AS total FROM ".TABLE_ADDRESS_BOOK." WHERE customers_id = '".(int) $_SESSION['customer_id']."' AND address_book_id = '".(int) $_SESSION['sendto']."'");

	if ($check_address->fields['total'] != '1') {
		$_SESSION['sendto'] = $_SESSION['customer_default_address_id'];
		if (isset ($_SESSION['shipping']))
			unset ($_SESSION['shipping']);
	}
}

if (!isset ($_SESSION['billto']))
	$_SESSION['billto'] = $_SESSION['customer_default_address_id'];
else {
	$check_address = $db->db_query("SELECT 
										COUNT(*) AS total 
									FROM 
										".TABLE_ADDRESS_BOOK." 
									WHERE 
										customers_id = '".(int)$_SESSION['customer_id']."' 
									AND 
										address_book_id = '".(int)$_SESSION['billto']."'");


	if ($check_address->fields['total'] != '1') {
		$_SESSION['billto'] = $_SESSION['customer_default_address_id'];
		if (isset ($_SESSION['payment']))
			unset ($_SESSION['payment']);
	}
}

if (!isset ($_SESSION['sendto']) || $_SESSION['sendto'] == "")
	$_SESSION['sendto'] = $_SESSION['billto'];

$order = new order();
if ($order->delivery['country']['iso_code_2'] != '')
	$_SESSION['delivery_zone'] = $order->delivery['country']['iso_code_2'];

require_once(DIR_WS_CLASSES.'class.payment.php');

if (!$is_xajax)	{
	if (isset ($_SESSION['credit_covers']))
		$_SESSION['payment'] = 'no_payment';
		
	$order_total_modules = new order_total();
	require_once(DIR_WS_CLASSES.'class.shipping.php');
	$shipping_modules = new shipping();
	$payment_modules = new payment();

	if (isset ($_GET['payment_error']) && is_object(${ $_GET['payment_error'] }) && ($error = ${$_GET['payment_error']}->get_error()))
		$smarty->assign('error', htmlspecialchars($error['error']));

}
$_SESSION['cartID'] = $_SESSION['cart']->cartID;

$virtual = $checkout->isVirtual();

$total_weight = $_SESSION['cart']->show_weight();
$total_count = $_SESSION['cart']->count_contents();

if (!$is_xajax) {
	$free_shipping = $checkout->isFreeShipping($order, $price);
	if ($free_shipping)
		include(DIR_WS_LANGUAGES.$_SESSION['language'].'/modules/order_total/ot_shipping.php');

	$quotes = $shipping_modules->quote();
}

if(!$is_xajax) 
	$payment_modules->update_status();

$any_out_of_stock = false;
if (STOCK_CHECK == 'true') {
	for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
		if (check_stock($order->products[$i]['id'], $order->products[$i]['qty']))
			$any_out_of_stock = true;
	}
	if ((STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true))
		redirect(href_link(FILENAME_SHOPPING_CART));
}

$css_file[] = 'templates/'.CURRENT_TEMPLATE.'/css/checkout.css';

$breadcrumb->add(NAVBAR_TITLE_1_CHECKOUT);
require(DIR_WS_INCLUDES.'xajax/xajax_core/xajax.inc.php');
include(DIR_WS_INCLUDES.'xajax/xajax.checkout.php');
require(DIR_WS_INCLUDES.'header.php');

$module_smarty = new Smarty();

if (!empty($_SESSION['shipping']['id']) && $_SESSION['shipping']['id'] != 'selfpickup_selfpickup' && !$is_xajax)
	if (is_object($GLOBALS['cash'])) $GLOBALS['cash']->enabled = false;
if (!empty($_SESSION['shipping']['id']) && $_SESSION['shipping']['id'] == 'selfpickup_selfpickup' && !$is_xajax)
	if (is_object($GLOBALS['cod'])) $GLOBALS['cod']->enabled = false;

if (!$is_xajax)
	$shipping_block = $checkout->getShippingBlock($price);

$check = explode(';', MODULE_SHIPPING_INSTALLED);
if(in_array('productsshipping.php', $check) || sizeof($check) <= 1)
	$smarty->assign('ONE_SHIPPING', ' dn');

$order = new order();
if (!$is_xajax)
	$payment_block = $checkout->getPaymentBlock($price, $payment_modules);
//echo_e($payment_block, 'E');
$check = explode(';', MODULE_PAYMENT_INSTALLED);
if(sizeof($check) <= 1)
	$smarty->assign('ONE_PAYMENT', ' dn');

if ($order->info['total'] <= 0)
	$get[] = 'gv_cover=true';

$cot_gv_stat = $_SESSION['cot_gv'] ? 1 : 0;

if (!$is_xajax) {
	$order = new order();
	$order_total_modules = new order_total();
	$order_total_modules->collect_posts();
	$order_total_modules->pre_confirmation_check();
//	$order_total_modules->process();
	if (ACTIVATE_GIFT_SYSTEM == 'true')
		$module_gift = $order_total_modules->credit_selection();
}

//Steps
$smarty->assign('TEXT_CHECKOUT_STEP_ONE', TEXT_CHECKOUT_STEP_1);
$smarty->assign('TEXT_CHECKOUT_STEP_TWO', TEXT_CHECKOUT_STEP_2);
$smarty->assign('TEXT_CHECKOUT_STEP_THREE', TEXT_CHECKOUT_STEP_3);
$smarty->assign('TEXT_CHECKOUT_STEP_FOUR', TEXT_CHECKOUT_STEP_4);

$smarty->assign('SHOW_STAT_PAYMENT_ADDRESS', $checkout->getStat('payment_address'));
$smarty->assign('SHOW_STAT_SHIPPING_ADDRESS', $checkout->getStat('shipping_address'));
$smarty->assign('SHOW_STAT_PAYMENT_MODULES', $checkout->getStat('payment_modules'));
$smarty->assign('SHOW_STAT_SHIPPING_MODULES', $checkout->getStat('shipping_modules'));
$smarty->assign('SHOW_STAT_COMMENTS', $checkout->getStat('comments'));
$smarty->assign('SHOW_STAT_PRODUCTS', $checkout->getStat('products'));
$smarty->assign('SHOW_STAT_AGB', $checkout->getStat('agb'));
$smarty->assign('SHOW_STAT_DSG', $checkout->getStat('dsg'));
$smarty->assign('SHOW_STAT_REVOCATION', $checkout->getStat('revocation'));

$smarty->assign('SHOW_STAT_PAYMENT_ADDRESS_SYMBOL', $checkout->getStatSymbol('payment_address'));
$smarty->assign('SHOW_STAT_SHIPPING_ADDRESS_SYMBOL', $checkout->getStatSymbol('shipping_address'));
$smarty->assign('SHOW_STAT_PAYMENT_MODULES_SYMBOL', $checkout->getStatSymbol('payment_modules'));
$smarty->assign('SHOW_STAT_SHIPPING_MODULES_SYMBOL', $checkout->getStatSymbol('shipping_modules'));
$smarty->assign('SHOW_STAT_COMMENTS_SYMBOL', $checkout->getStatSymbol('comments'));
$smarty->assign('SHOW_STAT_PRODUCTS_SYMBOL', $checkout->getStatSymbol('products'));
$smarty->assign('SHOW_STAT_AGB_SYMBOL', $checkout->getStatSymbol('agb'));
$smarty->assign('SHOW_STAT_DSG_SYMBOL', $checkout->getStatSymbol('dsg'));
$smarty->assign('SHOW_STAT_REVOCATION_SYMBOL', $checkout->getStatSymbol('revocation'));

$smarty->assign('FORM_ACTION', draw_form('checkout', $checkout->getFormUrl()));
$smarty->assign('FORM_END', '</form>');

$smarty->assign('CUSTOMERS_IP', $checkout->getIp());
$no_shippings = not_null(MODULE_SHIPPING_INSTALLED) ? 0 : 1;
$smarty->assign('NO_SHIPPINGS', $no_shippings);
$smarty->assign('FORM_ACTION_SHIPPING_MODULES', draw_form('form_shipping_modules', '', 'post', ''));
//echo_e($shipping_block, 'E');
$smarty->assign('SHIPPING_BLOCK', $shipping_block);
$smarty->assign('FREE_SHIPPING', $free_shipping ? '1' : '0');
if (!$free_shipping) $smarty->assign('FREE_SHIPPING_DESCRIPTION', sprintf(FREE_SHIPPING_DESCRIPTION, $price->format($price->_ext->getFreeShippingAmount(), true, 0, true)).draw_hidden_field('shipping', 'free_free'));
$smarty->assign('FREE_SHIPPING_ICON', $quotes[$i]['icon']);

$no_payments = not_null(MODULE_PAYMENT_INSTALLED) ? 0 : 1;
$smarty->assign('NO_PAYMENTS', $no_payments);
$smarty->assign('FORM_ACTION_PAYMENT_MODULES', draw_form('form_payment_modules', '', 'post', ''));
$smarty->assign('PAYMENT_BLOCK', $payment_block);
$smarty->assign('module_gift', $module_gift);
if($checkout->save_button)
	$smarty->assign('BUTTON_SAVE_PAYMENT_MODULE', $checkout->getSaveModuleButton('payment'));

$smarty->assign('VIRTUAL', $virtual);
$smarty->assign('TEXT_VIRTUAL', CHECKOUT_TEXT_VIRTUAL);
$smarty->assign('BUTTON_EDIT_SHIPPING_ADDRESS', $checkout->getEditAddressButton('shipping'));
$smarty->assign('BUTTON_BACK_SHIPPING_ADDRESS', $checkout->getBackButton('shipping'));
$smarty->assign('BUTTON_SAVE_SHIPPING_ADDRESS', $checkout->getSaveAddressButton('shipping'));
$smarty->assign('SHIPPING_ADDRESS_LABEL', address_label($_SESSION['customer_id'], $_SESSION['sendto'], true, ' ', '<br />'));
$smarty->assign('FORM_ACTION_SHIPPING_ADDRESS', draw_form('form_shipping_address', '', 'post', '').draw_hidden_field('action', 'process'));

// PAYMENT ADDRESS
$smarty->assign('BUTTON_EDIT_PAYMENT_ADDRESS', $checkout->getEditAddressButton('payment'));
$smarty->assign('BUTTON_BACK_PAYMENT_ADDRESS', $checkout->getBackButton('payment'));
$smarty->assign('BUTTON_SAVE_PAYMENT_ADDRESS', $checkout->getSaveAddressButton('payment'));
$smarty->assign('PAYMENT_ADDRESS_LABEL', address_label($_SESSION['customer_id'], $_SESSION['billto'], true, ' ', '<br />'));
$smarty->assign('FORM_ACTION_PAYMENT_ADDRESS', draw_form('form_payment_address', '', 'post', '').draw_hidden_field('action', 'process'));

if(TRUSTED_SHOP_STATUS == 'true')
	$smarty->assign('TRUSTED_SHOPS_CHECKOUT_DESC', TRUSTED_SHOPS_CHECKOUT_DESC);

// BOTH
require (DIR_WS_MODULES.'checkout_new_address.php');
$smarty->assign('ADDRESS_NEW_POSSIBLE', $checkout->newAddressPossible());

// PRODUCTS
$smarty->assign('PRODUCTS_BLOCK', $checkout->getProductsBlock());
//	$smarty->assign('PRODUCTS_BLOCK', $checkout->getProducts());
$smarty->assign('PRODUCTS_HOW_MUCH', $_SESSION['cart']->count_contents());

// COMMENTS
$smarty->assign('COMMENTS', draw_textarea_field('comments', 'soft', '60', '4', $_SESSION['comments'], 'style="width:99.8%"').draw_hidden_field('comments_added', 'YES'));

// AGB
$smarty->assign('AGB',$checkout->getAGB('text'));
$smarty->assign('AGB_link',$checkout->getAGB('link'));
$smarty->assign('AGB_checkbox',$checkout->getAGB('checkbox'));
$smarty->assign('AGB_stat',$checkout->getAGB('stat'));

// REVOCATION
$smarty->assign('REVOCATION',$checkout->getRevocation('text'));
$smarty->assign('REVOCATION_link',$checkout->getRevocation('link'));
$smarty->assign('REVOCATION_stat',$checkout->getRevocation('stat'));
$smarty->assign('REVOCATION_checkbox',$checkout->getRevocation('checkbox'));

// Datenschutz
$smarty->assign('DSG',$checkout->getDSG('text'));
$smarty->assign('DSG_link',$checkout->getDSG('link'));
$smarty->assign('DSG_stat',$checkout->getDSG('stat'));
$smarty->assign('DSG_checkbox',$checkout->getDSG('checkbox'));

// HIDDEN
$smarty->assign('HIDDEN_XAJAX',draw_hidden_field('checkout_xajax','1'));
if (is_object($GLOBALS[$_SESSION['payment']])) 	
	$smarty->assign('HIDDEN_PAYMENT', $GLOBALS[$_SESSION['payment']]->process_button());

// BUTTONS
$smarty->assign('BUTTON_ORDER','<a href="javascript:void(0)" id="submit_button">'.image_button('button_confirm_order.gif',IMAGE_BUTTON_CONFIRM_ORDER,'onclick="checkout();"').'</a>');
//$smarty->assign('BUTTON_ORDER_BACK','<a href="javascript:void(0)">'.image_button('button_back.gif',IMAGE_BUTTON_BACK,'onclick="javascript:history.back();"').'</a>');
$smarty->assign('BUTTON_ORDER_BACK','<a href="'. href_link(FILENAME_SHOPPING_CART) . '">'.image_button('button_back.gif',IMAGE_BUTTON_BACK).'</a>');

// TOTAL
if(!$is_xajax)
	$smarty->assign('ORDER_TOTAL', $checkout->getTotalBlock($order, $order_total_modules, $price));

if(isset($_GET['error_message']))
	$smarty->assign('error', urldecode($_GET['error_message']));
	
// MAIN
$smarty->assign('language', $_SESSION['language']);
$smarty->caching = false;
$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/checkout.html');
//echo_e($main_content, 'E');
$smarty->assign('main_content', $main_content);
$smarty->caching = false;
if (!defined('RM'))
	$smarty->loadFilter('output', 'note');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include ('includes/application_bottom.php');
?>
